
#-------------------------------------------------------------------------------
`
Run this function to creates figures related to the governments policies
`
#-------------------------------------------------------------------------------

#Load functions for simulations
include("01_Model_Structure.jl");
include("02_Functions.jl");
using Plots, JLD, Interpolations

#Load Economy
name_file = string("model_data/CE_model_structure_alpha_",0.028,".jld");
ce        = load(string(name_file), "ce");

#Choose values for plots
i_z_H      = searchsortedfirst(ce.z_grid,0.80)
i_z_L      = searchsortedfirst(ce.z_grid,0.20)
i_b_L      = searchsortedfirst(ce.b_grid,0.45)
i_b_H      = searchsortedfirst(ce.b_grid,0.80)
i_y_avg    = searchsortedfirst(ce.y_grid,1.00)
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
### Plots on default sets [heatmap]
#-------------------------------------------------------------------------------
p1 = Plots.heatmap(ce.y_grid, ce.b_grid, ce.default_P[:,:,i_z_H], xlabel="Output", ylabel="Debt")
Plots.savefig("figures/policies/defP.pdf")

p1 = Plots.heatmap(ce.y_grid, ce.b_grid, ce.default_I[:,:,i_z_H], xlabel="Output", ylabel="Debt")
Plots.savefig("figures/policies/defI.pdf")
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
### Plot on misreport [heatmap] [zoomed in]
#-------------------------------------------------------------------------------
A = similar(ce.πpol)
for (i_z, z_v) in enumerate(ce.z_grid)
    for (i_y, y_v)      in enumerate(ce.y_grid)
    for (i_b, b_v)      in enumerate(ce.b_grid)
	if ce.default_I[i_b,i_y,i_z]<=0.99
	A[i_b,i_y,i_z]  = ce.πpol[i_b,i_y,i_z];
	else
	A[i_b,i_y,i_z]  = ce.π_min;
	end
end
end
end
π_tmp  = ce.Π_exp[:,:,i_z_H].*(ce.default_I[:,:,i_z_H].<=1e-3)
knots  = (ce.b_grid, ce.y_grid)
π_int  = interpolate(knots, π_tmp,      Gridded(Linear()));

b_grid_finer = collect(linspace(ce.b_grid[i_b_L],ce.b_grid[end],100))
y_grid_finer = collect(linspace(ce.y_grid[1],ce.y_grid[end],100))
π            = π_int[b_grid_finer,y_grid_finer]
p1 = Plots.heatmap(y_grid_finer, b_grid_finer, π, xlabel="Output", ylabel="Debt")
Plots.savefig("figures/policies/pipol_reputation.pdf")
#-------------------------------------------------------------------------------


#-------------------------------------------------------------------------------
# Change in Reputation and Prices, for different values of {y,b} [heatmap]
#-------------------------------------------------------------------------------
knots        = (ce.b_grid, ce.y_grid, ce.z_grid)
qq_int       = interpolate(knots, ce.q_mat,     Gridded(Linear()));
b_grid_finer = collect(linspace(ce.b_grid[i_b_L],ce.b_grid[end],100))
y_grid_finer = collect(linspace(ce.y_grid[1],    ce.y_grid[end],100))
qq = qq_int[b_grid_finer,y_grid_finer,ce.z_grid[i_z_H]] - qq_int[b_grid_finer,y_grid_finer,ce.z_grid[i_z_L]]
p1 = Plots.heatmap(y_grid_finer, b_grid_finer, qq[:,:,1], xlabel="Output", ylabel="Debt")
Plots.savefig("figures/policies/DeltaPrices_Reputation.pdf")
#-------------------------------------------------------------------------------



#-------------------------------------------------------------------------------
## Bond prices, as a function of b
#-------------------------------------------------------------------------------
sizeF       = (600, 600)
p1 = Plots.plot(ce.b_grid[i_b_L:end], [ce.q_mat[i_b_L:end, 10, i_z_L] ce.q_mat[i_b_L:end, 10, i_z_H]], xlabel="Debt", ylabel="Bond Price",
label=["Low Reputation" "High Reputation"], color=[:black :black], linewidth=1.8, legend=:bottomleft, size=sizeF, xtickfont=font(12), ytickfont=font(12),
linestyle=[:solid :dash], size=(500,400))
xlims!(ce.b_grid[i_b_L], ce.b_grid[end])
Plots.savefig("figures/policies/debt_price_high_y.pdf")

p1 = Plots.plot(ce.b_grid[i_b_L:end], [ce.q_mat[i_b_L:end, 5, i_z_L] ce.q_mat[i_b_L:end, 5, i_z_H]], xlabel="Debt", ylabel="Bond Price",
label=["Low Reputation" "High Reputation"], color=[:black :black], linewidth=1.8, legend=:bottomleft, size=sizeF, xtickfont=font(12), ytickfont=font(12),
linestyle=[:solid :dash], size=(500,400))
xlims!(ce.b_grid[i_b_L], ce.b_grid[end])
Plots.savefig("figures/policies/debt_price_low_y.pdf")
#-------------------------------------------------------------------------------
